package com.gin.kotlin.no_gay_alliance.adapter.post

import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.argument.AttachmentArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemUploadBodyBinding
import com.gin.kotlin.no_gay_alliance.enums.HandlerMessageWhat
import com.gin.kotlin.no_gay_alliance.model.AttachmentModel
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import retrofit2.Call
import retrofit2.Response

class UploadedAttachmentAdapter(context: Context, handler: MyHandler, data: MutableList<AttachmentModel> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemUploadBodyBinding, AttachmentModel>(context, handler, false, data), View.OnClickListener {
    lateinit var attachmentArgument: AttachmentArgument

    override fun onBindViewHolder(holder: ViewHolder<ItemUploadBodyBinding>, position: Int, b: ItemUploadBodyBinding, dataModel: AttachmentModel) {
        val iv = b.ivUploadBody
        val tv = b.tvUploadBody
        listOf(iv, tv).forEach {
            it.setOnClickListener(this)
            it.tag = dataModel
        }

        configAttachmentImageView(iv,dataModel.extension){
            Glide.with(context)
                .load(dataModel.getUrl(dataModel.getImageThumbs()[0]))
                .error(R.drawable.ic_image_24)
                .placeholder(R.drawable.ic_image_24)
                .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
                .into(it)
        }
    }

    override fun onClick(v: View?) {
        val attachmentModel = v?.tag as? AttachmentModel ?: return
        val index = data.indexOf(attachmentModel)
        when (v.id) {
            R.id.iv_upload_body -> {
                handler.sendMessage(HandlerMessageWhat.ON_CLICK_ATTACHMENT, attachmentModel)
            }

            R.id.tv_upload_body -> {
                // 删除附件
                AlertDialogUtils.builder(context, context.getString(R.string.dialog_title_delete_attachment), listener = { _, which ->
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        // 请求删除附件
                        App.INSTANCE.ngaApi.postApi.attachDel(
                            filename = attachmentModel.filename!!,
                            topicId = attachmentArgument.topicId!!,
                            replyId = attachmentArgument.replyId,
                        ).enqueue(object : NgaCallback<List<String>> {
                            override fun onSuccess(
                                call: Call<NgaResponse<List<String>>>,
                                response: Response<NgaResponse<List<String>>>,
                                body: NgaResponse<List<String>>?,
                                data: List<String>?
                            ) {
                                handler.toast(data?.get(0))
                                removeData(index)
                                handler.sendMessage(HandlerMessageWhat.ON_DELETED_ATTACHMENT, attachmentModel)
                            }
                        })
                    }
                }).show()
            }
        }
    }

    companion object {
        /**
         * 配置一个表示附件的 ImageView 内容
         * @param iv ImageView
         * @param extension 后缀
         * @param elseDo 非mp3 mp4 zip 时的行为
         */
        fun configAttachmentImageView(iv: ImageView, extension: String? = null, elseDo:(iv:ImageView)->Unit) {
            when (extension) {
                "mp3" -> iv.setImageResource(R.drawable.ic_audio_24)
                "mp4" -> iv.setImageResource(R.drawable.ic_video_24)
                "zip" -> iv.setImageResource(R.drawable.ic_zip)
                else -> elseDo.invoke(iv)
            }
        }
    }
}